package com.lb.jwitter.hibernate;

import org.apache.commons.logging.Log;
import org.hibernate.Query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.lb.utils.orm.hibernate.HibernateGenericDao;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.Tbl_ThongTin_HocVien_MonHoc;

@Repository("tbl_ThongTin_HocVien_MonHocDAO")
public class Tbl_ThongTin_HocVien_MonHocHibernateDAO extends HibernateGenericDao<Tbl_ThongTin_HocVien_MonHoc, Long> implements Tbl_ThongTin_HocVien_MonHocDAO {
   private Log log = LogFactory.getLog(Tbl_ThongTin_HocVien_MonHocHibernateDAO.class);

   @Autowired
   public Tbl_ThongTin_HocVien_MonHocHibernateDAO(@Qualifier("sessionFactory")   SessionFactory sessionFactory) {
       super(sessionFactory);
   }

   @SuppressWarnings("unchecked")
@Transactional
   public final List<Tbl_ThongTin_HocVien_MonHoc> findAll(int pageSize, int page) {
       if( pageSize == 0) return getSession().createCriteria(Tbl_ThongTin_HocVien_MonHoc.class).list(); 
       return getSession().createCriteria(Tbl_ThongTin_HocVien_MonHoc.class).setFirstResult(pageSize * (page-1)).setMaxResults(pageSize).list();
   }
   
@Transactional
   public final Long save(Tbl_ThongTin_HocVien_MonHoc _newInstance) {
   	return super.save(_newInstance);
   }

@Transactional
   public final boolean exists(Long id) {
       boolean result = (Integer) getSession().createCriteria(Tbl_ThongTin_HocVien_MonHoc.class).setProjection(Projections.rowCount()).add(
                       Restrictions.eq("id", id)).uniqueResult() == 1;
       log.info("Result: "+result);
   	return result;
   }

@Transactional
   public Collection<Tbl_ThongTin_HocVien_MonHoc> find(Tbl_ThongTin_HocVien_MonHoc obj,int pageSize,int page){
   	Collection<Tbl_ThongTin_HocVien_MonHoc> _tbl_ThongTin_HocVien_MonHocs = new ArrayList<Tbl_ThongTin_HocVien_MonHoc>();
   	try {
   		String sql = "select d from tbl_ThongTin_HocVien_MonHoc as d where 1=1 ";
   sql += " and d.id = " + obj.getId() + " ";
   sql += " and d.hocVienId. = " + obj.getHocVienId().getId() + " ";
   sql += " and d.monHocId. = " + obj.getMonHocId().getId() + " ";
       if(obj.getTrangThai() != null && !obj.getTrangThai().equalsIgnoreCase("")){
   		sql +=  " and d.trangThai like '%" +obj.getTrangThai() + "%' " ;
   }
   		sql += " order by d.id DESC ";
   		_tbl_ThongTin_HocVien_MonHocs = getSession().createQuery(sql).setFirstResult(pageSize*(page-1)).setMaxResults(pageSize).list();
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
   	return _tbl_ThongTin_HocVien_MonHocs;
   }
   

	public final String CalSPKiemTraKetQuaMonHoc(String mssv, String maMonHoc ){
		String kq = null;
		try {
			Query query =  getSession().createSQLQuery("CALL KiemTraKetQuaMonHoc (:mssv, :maMonHoc)");
			query.setParameter("mssv", mssv);
			query.setParameter("maMonHoc", maMonHoc);

			List result = query.list();
			for(int i=0; i<result.size(); i++){
				kq = (String) result.get(i);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return kq;
	}

}//endclass
